package com.meta.nd.service.impl;

import com.meta.cache.NdCacheService;
import com.meta.common.CommonInfo;
import com.meta.common.ConstDef;
import com.meta.nd.service.DbMapService;
import com.meta.nd.service.NvService;
import com.meta.nd.util.NdDbMapping;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class DbMapServiceImpl implements DbMapService {
    @Resource
    NdCacheService ndCacheService;
    @Resource
    NvService nvService;
@Override
public NdDbMapping getDbMap(String dbMapCode) {
    Nv nv = this.nvService.getNvByCode(CommonInfo.getDefault(),dbMapCode, ConstDef.FD_NAME_DB_MAP_CODE,ConstDef.ND_CODE_TYPE_MAPPING_ND);
    return transferToDbMap(nv);
}

    @Override
    public Nv createDefaultDbMapNv(String ndCode){
        Nd nd = ndCacheService.getNdRequired(ndCode);
        NdDbMapping dbMapping = new NdDbMapping();
        dbMapping.setDbMapCode(ndCode);
        dbMapping.setName(nd.getNdName());
        dbMapping.setDescription(nd.getNdDescription());
        dbMapping.setNvTableName("T_" + nd.getNdName());
        dbMapping.setNvFieldNames(nd.getFieldNames());
        dbMapping.setFvTableName("T_" + nd.getNdName() + "_Extension");
        dbMapping.setPersistRemainFields(false);
        return transferDbMapToNv(dbMapping);
    }

private NdDbMapping transferToDbMap(Nv nv){
    String dbMapCode = nv.getRawStringByName(ConstDef.FD_NAME_DB_MAP_CODE);
    String nvTableName = nv.getRawStringByName(ConstDef.FD_NAME_NV_TABLE_NAME);
    String fvTableName = nv.getRawStringByName(ConstDef.FD_NAME_fV_TABLE_NAME);
    String nvTableFieldNames = nv.getRawStringByName(ConstDef.FD_AME_NV_FIELD_NAMES);
    String fvTableFieldNames = nv.getRawStringByName(ConstDef.FD_AME_FV_FIELD_NAMES);
    boolean persistRemain = nv.getRawBoolByName(ConstDef.FD_NAME_PERSIST_REMAIN);
    NdDbMapping dbMapping = new NdDbMapping(dbMapCode,nvTableName,fvTableName,nvTableFieldNames,fvTableFieldNames,persistRemain);
    dbMapping.setName(nv.getRawStringByName(ConstDef.FD_NAME_NAME));
    dbMapping.setDescription(nv.getRawStringByName(ConstDef.FD_NAME_DESCRIPTION));
    return dbMapping;
}

    private Nv transferDbMapToNv(NdDbMapping dbMapping){
        Nv nv = nvService.initNv(CommonInfo.getDefault(),ConstDef.ND_CODE_TYPE_MAPPING_ND);
        nv.setStringFvByName(ConstDef.FD_NAME_DB_MAP_CODE,dbMapping.getDbMapCode());
        nv.setStringFvByName(ConstDef.FD_NAME_NAME,dbMapping.getName());
        nv.setStringFvByName(ConstDef.FD_NAME_DESCRIPTION,dbMapping.getDescription());
        nv.setStringFvByName(ConstDef.FD_NAME_NV_TABLE_NAME,dbMapping.getNvTableName() );
        nv.setStringFvByName(ConstDef.FD_NAME_fV_TABLE_NAME,dbMapping.getFvTableName());

        nv.setStringFvByName(ConstDef.FD_AME_NV_FIELD_NAMES,listToString(dbMapping.getNvFieldNames()));
        nv.setStringFvByName(ConstDef.FD_AME_FV_FIELD_NAMES,listToString(dbMapping.getFvFieldNames()));
        nv.setBoolFvByName(ConstDef.FD_NAME_PERSIST_REMAIN,dbMapping.isPersistRemainFields());
        return nv;
    }

    public static String listToString(List<String> list) {
        return list.stream().collect(Collectors.joining(","));
    }

}
